package cc.rengu.igas.smps.core.service.trans;

import cc.rengu.igas.smps.common.constant.SmpsParamConstant;
import cc.rengu.igas.smps.facade.request.AliyunIotMsgPushRequest;
import cc.rengu.igas.smps.facade.response.AliyunIotMsgPushResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.utility.base.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.HttpClientConfig;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.codec.binary.Base64;

/**
 * 信雅达语音消息推送服务
 * Created by wangcheng on 2021/4/2 10:34
 */
public class AliyunIotMsgPushService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        AliyunIotMsgPushRequest aliyunIotMsgPushRequest = new AliyunIotMsgPushRequest();
        AliyunIotMsgPushResponse aliyunIotMsgPushResponse = new AliyunIotMsgPushResponse();
        try {
            ConvertUtil.convertOutput(aliyunIotMsgPushRequest);
            aliyunIotMsgPushResponse.setHeader(aliyunIotMsgPushRequest.getHeader());
            String productKey = aliyunIotMsgPushRequest.getProductKey();
            String deviceName = aliyunIotMsgPushRequest.getDeviceName();
            String voiceMsg = aliyunIotMsgPushRequest.getVoiceMsg();
            if (StringUtil.isNullorEmpty(productKey) || StringUtil.isNullorEmpty(deviceName) || StringUtil.isNullorEmpty(voiceMsg)) {
                rglog.error("缺少一个或多个业务参数！");
                aliyunIotMsgPushResponse.setRespCode(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode());
                aliyunIotMsgPushResponse.setRespDesc(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
            } else {
                DefaultProfile profile = DefaultProfile.getProfile(SmpsParamConstant.REGION_ID, SmpsParamConstant.ACCESS_KEY_ID, SmpsParamConstant.ACCESS_SECRET);
                SysParamService sysParamService = new SysParamServiceImpl();
                SysParam sysParam = sysParamService.getSysParamInfo(AppParamConstant.DEFAULT_INSTID, "SMPS_ALIYUN_IOT", "PROXY_ADDR");
                HttpClientConfig httpClientConfig = new HttpClientConfig();
                httpClientConfig.setHttpProxy(sysParam.getParamValue());
                httpClientConfig.setCompatibleMode(true);
                profile.setHttpClientConfig(httpClientConfig);
                IAcsClient client = new DefaultAcsClient(profile);
                CommonRequest request = new CommonRequest();
                request.setSysMethod(MethodType.POST);
                request.setSysDomain(SmpsParamConstant.SYS_DOMAIN);
                request.setSysVersion(SmpsParamConstant.SYS_VERSION);
                request.setSysAction(SmpsParamConstant.SYS_ACTION);
                request.putQueryParameter("RegionId", SmpsParamConstant.REGION_ID);
                request.putQueryParameter("Timeout", SmpsParamConstant.DEFAULT_TIME_OUT);
                request.putQueryParameter("DeviceName", aliyunIotMsgPushRequest.getDeviceName());
                request.putQueryParameter("RequestBase64Byte", Base64.encodeBase64String(aliyunIotMsgPushRequest.getVoiceMsg().getBytes()));
                request.putQueryParameter("ProductKey", aliyunIotMsgPushRequest.getProductKey());
                try {
                    CommonResponse response = client.getCommonResponse(request);
                    String data = response.getData();
                    //解析Json字符串，判断success状态
                    if (!StringUtil.isNullorEmpty(data)) {
                        JSONObject dataJson = JSONObject.parseObject(data);
                        String flag = dataJson.getString("Success");
                        if ("true".equals(flag)) {
                            aliyunIotMsgPushResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                            aliyunIotMsgPushResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                        } else {
                            aliyunIotMsgPushResponse.setRespCode(dataJson.getString("Code"));
                            aliyunIotMsgPushResponse.setRespDesc(dataJson.getString("ErrorMessage"));
                        }
                    } else {
                        aliyunIotMsgPushResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                        aliyunIotMsgPushResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
                    }
                } catch (ClientException e) {
                    rglog.error(e.getMessage());
                    //失败赋值应答码为明确失败及应答消息直接退出
                    rglog.error("{}", StringUtil.ExceptionToString(e));
                    aliyunIotMsgPushResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                    aliyunIotMsgPushResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
                }
            }
            ConvertUtil.convertInput(aliyunIotMsgPushResponse);
        } catch (Exception e) {
            //失败赋值应答码为明确失败及应答消息直接退出
            rglog.error("{}", StringUtil.ExceptionToString(e));
            aliyunIotMsgPushResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            aliyunIotMsgPushResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            return -1;
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
